/**
 * @copyright Copyright (c) 2025 CSG Digital Grid Technology (Guangdong) Co., Ltd.
 * @author Xie Jinhui
 */

#include "main.h"
#include "cpu.h"
#define SOFTWARE_VERSION "2.1.1"

/**
 * @brief help
 *
 */
static void help(void)
{

    printf("devinfo{ -h | -t | -e | -M | -v | -V | -I | -m | -c | -f | -s | -o | -w | -n | -a }\n\
-h    --help\n\
-t    --show-type\n\
-e    --show-esn\n\
-M    --show-mac\n\
-v    --show-version\n\
-V    --show-vendor\n\
-I    --show-internal-storage-size\n\
-m    --show-memory-size\n\
-c    --show-cpu-type\n\
-f    --show-cpu-frequency\n\
-s    --show-firmware-version\n\
-o    --show-overlay-version\n\
-w    --update-config-information\n\
-n    --show-NodeId\n\
-a    --show-all\n");
}

/**
 * @brief Get the Option Param object
 *
 * @param argc
 * @param argv
 * @return uint8_t 
 */
uint8_t getOptionParam(int argc, char **argv)
{
    int ch;
    char esn_buffer[128] = {0};
    char mac_buffer[1024] = {0};
    char cpu_type[1024] = {0};
    int32_t fre_val = 0;
    int32_t mem_val = 0;
    uint16_t i = 0;
    long flash_size = 0;
    char flash_size_str[128] = {0};
    char *output_buffer = NULL;

    /* DTU项目去掉esn和mac地址得显示 */
    ch = getopt(argc, argv, "hteMvVImcfsanow:");
    if (ch == -1)
    {
        printf("Command parameter error!\n");
        help();
        return FALSE;
    }

    switch (ch)
    {
    case 'h':
        help();
        return TRUE;
        break;

    case 't':
        if (!get_configuration())
        {
            printf("main: get_configuration error!\n");
            return FALSE;
        }

        printf("device type: %s\n", deviceConfig.deviceType);
        break;

    case 'e':
        //获取固件类型
        if (!get_esn(esn_buffer))
        {
            printf("getOptionParam: get_esn error!\n");
            return FALSE;
        }
        else
        {
            printf("%s", esn_buffer);
            return TRUE;
        }
        break;

    case 'M':
        if (!get_mac(mac_buffer))
        {
            printf("getOptionParam: get_mac error!\n");
            return FALSE;
        }
        else
        {
            printf("%s", mac_buffer);
            return TRUE;
        }
        break;

    case 'v':
        printf("version: V%s\n", SOFTWARE_VERSION);
        return TRUE;
        break;

    case 'V':
        if (!get_configuration())
        {
            printf("main: get_configuration error!\n");
            return FALSE;
        }

        printf("vendor: %s\n", deviceConfig.vendor);
        return TRUE;
        break;

    case 'I':
        sprintf(flash_size_str, "Flash size: %ldM\n", get_flashSize());
        printf("%s", flash_size_str);
        return TRUE;
        break;

    case 'm':
        mem_val = get_memory();
        if (mem_val == -1)
        {
            printf("Memory size: \n");
        }
        else
        {
            printf("Memory size: %dM\n", mem_val);
        }
        break;

    case 'c':
        if (get_cpu_type(cpu_type)  != 0)
        {
            printf("Get CPU Type failed\n");
            return FALSE;
        }
        else
        {
            printf("CPU Type: %s\n", cpu_type);
        }

        break;

    case 'f':
        fre_val = get_cpuFreq();
        printf("CPU(MHz): %d\n", fre_val);
        break;

    case 's':
        if (!get_configuration())
        {
            printf("main: get_configuration error!\n");
            return FALSE;
        }

        printf("firmware version: %s\n", deviceConfig.firmwareVersion);
        break;

    case 'o':
        if (!get_configuration())
        {
            printf("main: get_configuration error!\n");
            return FALSE;
        }

        printf("overlay version: %s\n", deviceConfig.factureOverlayVersion);
        break;

    case 'n':
        if (!get_configuration())
        {
            printf("main: get_configuration error!\n");
            return FALSE;
        }

        printf("NodeId: %s\n", deviceConfig.nodeId);
        break;

    case 'w':
        if(set_configinfo(optarg))
        {
            printf("main: set_configinfo error!\n");
            return FALSE;
        }
        break;

    case 'a':
        output_buffer = (char *)malloc(2048);

        if (!get_configuration())
        {
            printf("ERROR: get_configuration error!\n");
        }

        if (!get_esn(esn_buffer))
        {
            printf("get_esn error!\n");
        }

        if (!get_mac(mac_buffer))
        {
            printf("get_mac error!\n");
        }

        flash_size = get_flashSize();

        mem_val = get_memory();
        if (mem_val == -1)
        {
            printf("ERROR: get_memory error!\n");
        }

        if (get_cpu_type(cpu_type) != 0)
        {
            printf("get_cpu_type error!\n");
        }

#ifdef FUXI /* fuxi不支持查询cpu频率, 写死800M */
        fre_val = 800;
#else
        fre_val = get_cpuFreq();
#endif
        sprintf(output_buffer, "device type: %s\n%s%svendor: %s\nMemory size: %dM\nFlash size: %ldM\nCPU Type: %s\nCPU(MHz): %d\noverlay version: %s\nfirmware version: %s\nNodeId: %s\n",
              deviceConfig.deviceType,
              esn_buffer,
              mac_buffer,
              deviceConfig.vendor,
              mem_val,
              flash_size,
              cpu_type,
              fre_val,
              deviceConfig.factureOverlayVersion,
              deviceConfig.firmwareVersion,
              deviceConfig.nodeId);

        printf("%s", output_buffer);

        free(output_buffer);
        output_buffer = NULL;

        break;

    default:
        help();
        break;
    }

    return TRUE;
}
